package com.biz.controller.mall;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.biz.conf.Global;
import com.biz.model.Pmodel.basic.*;
import com.biz.model.Hmodel.basic.TwxGoodsGroup;
import com.biz.model.Pmodel.Ppics;
import com.biz.model.Pmodel.PwxGood;
import com.biz.model.Pmodel.PwxGoods;
import com.biz.model.Pmodel.User;
import com.biz.model.Pmodel.basic.Pwxgoods;
import com.biz.model.Pmodel.basic.PwxgoodsProperty;
import com.biz.service.mall.MallServiceI;
import com.framework.controller.BaseController;
import com.framework.model.MessageBox;
import com.framework.model.Pager;
import com.framework.model.Paging;
import com.framework.utils.ConfigUtil;
import com.framework.utils.StringUtil;
import com.framework.utils.URLConectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商户设置相关
 * Created by liujiajia on 2016/11/14.
 */
@Controller
@RequestMapping("/mall")
public class MallController extends BaseController{


    @InitBinder("pwxgoods")
    public void initBinderFormBean1(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("pwxgoods.");
    }

    @Autowired
    private MallServiceI mallService;


    /**
     * 跳转联盟商品
     * @param mv
     * @return
     */
    @RequestMapping("toMall_lm")
    public ModelAndView toMall_lm(ModelAndView mv)throws Exception{
        List<Map<String,Object>> list=mallService.findBrandSpeed();
        mv.addObject("list",list);
        mv.setViewName("basic/mall/mall_lm");
        return mv;
    }

    /**
     * 跳转商品编辑
     * @param mv
     * @return
     */
    @RequestMapping("mall_lmEdit")
    public ModelAndView mall_lmEdit(ModelAndView mv)throws Exception{
        mv.addObject("page_type","insert");
        mv.setViewName("basic/mall/mall_lmEdit");
        return mv;
    }

    /**
     * 展示商户列表信息
     * @param response
     * @param request
     */
    @RequestMapping("showUnionMall")
    public void showUnionMall(HttpServletResponse response, HttpServletRequest request)throws Exception{
        //必要的分页参数
        Map<String,Object> params=new HashMap<String, Object>();
        params.put("name", request.getParameter("name")==null?"":request.getParameter("name"));
        params.put("groupId", request.getParameter("groupId")==null?"":request.getParameter("groupId"));
        params.put("shangxiajia", request.getParameter("shangxiajia")==null?"":request.getParameter("shangxiajia"));
        //todo add shopId in params
        params.put("isTicket","1");
        params.put("order",request.getParameter("order"));
        params.put("speed_code",request.getParameter("speed_code"));
        Pager<Pwxgoods> pager = new Pager();
        pager.setParameters(params);
        pager.setStartRecord(Integer.parseInt(request.getParameter("offset")));
        pager.setPageSize(Integer.parseInt(request.getParameter("limit")));

        pager=mallService.queryMall(pager);

        Paging<Pwxgoods> paging = new Paging<>();
        paging.setRows(pager.getExhibitDatas());
        paging.setTotal((long) pager.getRecordCount());

        writeJson(paging,response);
    }

    /**
     * 调用接口加载商户信息
     */
    @RequestMapping("/loadBrand")
    public void loadBrand(HttpServletResponse response) throws Exception {
        List<Pbrand> list= mallService.loadBrand();
        writeJson(list, response);

    }

    /**
     * 保存商品
     * @param response
     * @param pwxgoods
     * @throws Exception
     */
    @RequestMapping("doSave")
    public void doSave(HttpServletResponse response,Pwxgoods pwxgoods)throws Exception {
        //判断库存中有没有空值
        Map<String, Object> map=new HashMap<>();
            try {
                 map=mallService.saveGoodsAndStock(pwxgoods);
            } catch (Exception e) {
                map.put("isok","no");
                map.put("msg","系统异常");
            }
            writeJson(map,response);
    }

    /**
     * 跳转商品修改
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unused")
    @RequestMapping("toEdit")
    public ModelAndView toEdit(String id) throws Exception {
        mv.clear();
        //通过选择的品种id 加载详情
        Pwxgoods twxgoods = mallService.getByIdBySql(id);
        //属性集合
        List<PwxgoodsProperty> propertyList = mallService.getwxgoodsProperty(id);
        //此品种所有图片信息
        List<Ppics> picslist = mallService.findPicsById(id);
        mv.addObject("piclist", picslist);
        mv.addObject("twxgoods", twxgoods);
        mv.addObject("page_type", "update");
        mv.addObject("info", twxgoods.getInfo());
        mv.setViewName("basic/mall/mall_lmEdit");
        return mv;
    }


    /**
     * 更具id 删除商户信息
     * @param ids
     * @param response
     */
    @RequestMapping("delGridById")
    public void delGridById(String ids,HttpServletResponse response)throws Exception{
        MessageBox box=new MessageBox();
        try{
            if (ids == null) {
                box.setSuccess(false);
                box.setMsg("未指定");
            } else {
                mallService.delGridById(ids);
            }
        }catch (Exception e){
            box.setSuccess(false);
        }
        writeJson(box, response);
    }



    @RequestMapping("deByIds")
    public void delGridById(HttpServletResponse response, HttpServletRequest request,String ids) throws Exception {
        MessageBox mb = getBox();
        try{
            mallService.executeHql(StringUtil.formateString("update Twxgoods set isdel =1 where id in ({0})",StringUtil.formatSqlIn(ids)));
            mb.setSuccess(true);
        }catch (Exception e){
            mb.setSuccess(false);
        }
        writeJson(mb,response);
    }


    @RequestMapping("upOrDown")
    public void upOrdown(HttpServletResponse response, String ids, String state, String type) throws Exception {
        MessageBox mb = getBox();
        try {
            User user = (User) getShiroAttribute("userinfo");
            mallService.upOrdown(ids, state, user.getId(), type);
            mb.setSuccess(true);
        }catch (Exception e){
            mb.setSuccess(false);
        }
        writeJson(mb,response);
    }


}
